<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-present http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Released under the MIT License.
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------

namespace app\pay\controller;

use cmf\controller\AdminBaseController;
use think\db\Query;
use think\facade\Db;

class ScoreController extends AdminBaseController
{

    /**
     * Notes:积分商品列表
     * User:
     * DateTime: 2022-01-17 14:57
     * @return mixed
     * @throws \think\db\exception\DbException
     */
    public function index()
    {
        $param = $this->request->param();
        $list = Db::name("jf_product")
            ->where(function (Query $query) use ($param) {
                $query->where(['delete_time' => 0]);
                if (isset($param['title']) && !empty($param['title'])) {
                    $query->where('title', 'like', "%" . $param['title'] . "%");
                }
                if (isset($param['status']) && !empty($param['status'])) {
                    $query->where(['status' => $param['status']]);
                }
            })
            ->order("id desc")
            ->paginate(30);
        $page = $list->render();
        $this->assign("page", $page);
        $this->assign("list", $list);
        $this->assign('title', isset($param['title']) ? $param['title'] : '');
        $this->assign('status', isset($param['status']) ? $param['status'] : '');
        return $this->fetch();
    }

    /**
     * Notes:添加积分商品页面
     * User:
     * DateTime: 2022-01-17 16:53
     * @return mixed
     */
    public function add()
    {
        return $this->fetch();
    }

    /**
     * Notes:执行添加积分商品
     * User:
     * DateTime: 2022-01-17 16:58
     */
    public function addPost()
    {
        $param = $this->request->param();
        $data['title'] = $param['title'];
        $data['category_id'] = 5;
        $data['desc'] = $param['desc'];
        $data['image'] = $param['image'];
        if (!empty($param['photo_names']) && !empty($param['photo_urls'])) {
            $urls = [];
            foreach ($param['photo_urls'] as $k => $v) {
                $urls[$k] = $v;
            }
            $data['photos'] = json_encode($urls);
        }
        $data['content'] = $param['content'];
        $data['status'] = 1;
        $data['price'] = $param['price'];
        $data['stock'] = $param['stock'];
        $data['sales'] = $param['sales'];
        $data['create_time'] = date("Y-m-d H:i:s");
        $res = Db::name("jf_product")->save($data);
        if (!$res) $this->error("失败！");
        $this->success("成功！");
    }

    /**
     * Notes:编辑积分商品页面
     * User:
     * DateTime: 2022-01-17 17:11
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit()
    {
        $param = $this->request->param();
        $info = Db::name("jf_product")->where(["id" => $param['id']])->find();
        $photos = json_decode($info['photos'], 1);
        $this->assign("info", $info);
        $this->assign("photos", $photos);
        return $this->fetch();
    }

    /**
     * Notes:执行编辑积分商品
     * User:
     * DateTime: 2022-01-17 16:58
     */
    public function editPost()
    {
        $param = $this->request->param();
        $data['title'] = $param['title'];
        $data['desc'] = $param['desc'];
        $data['image'] = $param['image'];
        if (!empty($param['photo_names']) && !empty($param['photo_urls'])) {
            $urls = [];
            foreach ($param['photo_urls'] as $k => $v) {
                $urls[$k] = $v;
            }
            $data['photos'] = json_encode($urls);
        }
        $data['content'] = $param['content'];
        $data['price'] = $param['price'];
        $data['stock'] = $param['stock'];
        $data['sales'] = $param['sales'];
        $data['update_time'] = time();
        $res = Db::name("jf_product")->where(["id" => $param['id']])->save($data);
        if (!$res) $this->error("失败！");
        $this->success("成功！");
    }

    /**
     * Notes:启用/停用
     * User:
     * DateTime: 2022-01-17 15:10
     * @throws \think\db\exception\DbException
     */
    public function publish()
    {
        $param = $this->request->param();
        if (isset($param['id'])) {
            $id = $this->request->param('id', 0, 'intval');
            Db::name("jf_product")->where('id', $id)->update(['status' => $param['status'], 'update_time' => time()]);
            $this->success("成功！", '');
        }

        if (isset($param['ids']) && isset($param["yes"])) {
            $ids = $this->request->param('ids/a');
            Db::name("jf_product")->where('id', 'in', $ids)->update(['status' => 1, 'update_time' => time()]);
            $this->success("成功！", '');
        }
    }

    /**
     * Notes:删除
     * User:
     * DateTime: 2022-01-17 15:10
     * @throws \think\Exception
     */
    public function delete()
    {
        $this->deleteInfo("jf_product");
        $this->success("删除成功");
    }

    /**
     * Notes:积分订单
     * User:
     * DateTime: 2022-01-17 14:57
     * @return mixed
     * @throws \think\db\exception\DbException
     */
    public function order()
    {
        $param = $this->request->param();
        $list = Db::name("jf_order")->alias("o")
            ->field("o.*,p.title,p.image,u.user_login")
            ->join("jf_product p", "o.product_id = p.id", "left")
            ->join("user u", "o.user_id = u.id", "left")
            ->where(function (Query $query) use ($param) {
                if (isset($param['title']) && !empty($param['title'])) {
                    $query->where('p.title', 'like', "%" . $param['title'] . "%");
                }
                if (isset($param['user_id']) && !empty($param['user_id'])) {
                    $query->where(['o.user_id' => $param['user_id']]);
                }
                if (isset($param['status']) && !empty($param['status'])) {
                    $query->where(['o.status' => $param['status']]);
                }
                if (isset($param['start_time']) && !empty($param['start_time'])) {
                    if (!isset($param['end_time']) || empty($param['end_time'])) {
                        $param['end_time'] = time();
                    }
                    $query->whereTime('o.create_time', 'between', [$param['start_time'], $param['end_time']]);
                }
            })
            ->order("o.id desc")
            ->paginate(30);
        $page = $list->render();
        $this->assign("page", $page);
        $this->assign("list", $list);
        $this->assign('title', isset($param['title']) ? $param['title'] : '');
        $this->assign('user_id', isset($param['user_id']) ? $param['user_id'] : '');
        $this->assign('status', isset($param['status']) ? $param['status'] : '');
        $this->assign('start_time', isset($param['start_time']) ? $param['start_time'] : '');
        $this->assign('end_time', isset($param['end_time']) ? $param['end_time'] : '');
        return $this->fetch();
    }

    /**
     * 发货
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function give()
    {
        $param = $this->request->param();
        $info = Db::name("jf_order")->where(["id" => $param['id']])->find();
        $this->assign("info", $info);
        return $this->fetch();
    }

    /**
     * 执行发货
     * @return void
     */
    public function givePost()
    {
        $param = $this->request->param();
        $data['express_name'] = $param['express_name'];
        $data['express_code'] = $param['express_code'];
        $data['status'] = 1;
        $data['update_time'] = time();
        $res = Db::name("jf_order")->where(["id" => $param['id']])->save($data);
        if (!$res) $this->error("失败！");
        $this->success("成功！");
    }

    /**
     * 完成
     * @return void
     * @throws \think\db\exception\DbException
     */
    public function done()
    {
        $param = $this->request->param();
        if (isset($param['id'])) {
            $id = $this->request->param('id', 0, 'intval');
            Db::name("jf_order")->where('id', $id)->update(['status' => 2, 'update_time' => time()]);
            $this->success("成功！");
        }
    }

    /**
     * 积分日志
     * @return mixed
     * @throws \think\db\exception\DbException
     */
    public function scoreLog()
    {
        $param = $this->request->param();
        $list = Db::name("jf_changes")->alias('r')
            ->field("r.*,m.user_login")
            ->leftJoin("user m", "r.user_id = m.id")
            ->where(function (Query $query) use ($param) {
                if (isset($param['start_time']) && !empty($param['start_time'])) {
                    if (!isset($param['end_time']) || empty($param['end_time'])) {
                        $param['end_time'] = time();
                    }
                    $query->whereTime('r.create_time', 'between', [$param['start_time'], $param['end_time']]);
                }
                if (isset($param['user_login']) && !empty($param['user_login'])) {
                    $query->where('m.user_login', 'like', "%" . $param['user_login'] . "%");
                }
                if (isset($param['type']) && $param['type'] != '') {
                    $query->where(['r.type' => $param['type']]);
                }
            })
            ->order("r.id desc")
            ->paginate(30);
        $page = $list->render();
        $this->assign("page", $page);
        $this->assign("list", $list);
        $this->assign('start_time', isset($param['start_time']) ? $param['start_time'] : '');
        $this->assign('end_time', isset($param['end_time']) ? $param['end_time'] : '');
        $this->assign('user_login', isset($param['user_login']) ? $param['user_login'] : '');
        $this->assign('type', isset($param['type']) ? $param['type'] : '');
        return $this->fetch();
    }

}
